package com.testflow.hyh.util;

import com.testflow.hyh.testReport.V1.ReportInfo;
import com.testflow.hyh.testReport.V2.ApiInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.util.*;

/**
 * @Author: 何宇航
 * @Description：
 * @DateTime：2022/6/13 0013 13:41
 */
@Slf4j
@Component
public class MailUtils {
    public static void send163Mail(String address, String headline, ReportInfo reportInfo) {
        //Thread thread = new Thread(() -> {
            Properties properties = null;
            try {
                // 读取配置文件
                properties = PropertiesLoaderUtils.loadAllProperties("properties/mail.properties");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            //auth认证
            Properties finalProperties = properties;
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    //发送者邮箱以及授权密码
                    return new PasswordAuthentication(finalProperties.getProperty("fromMail") , finalProperties.getProperty("password"));
                }
            });
            // 开启Debug日志
            session.setDebug(false);
            try {
                log.info("【邮件开始投递】");
                Transport transport = session.getTransport();
                transport.connect();
                MimeMessage mimeMessage = new MimeMessage(session);
                mimeMessage.setFrom(new InternetAddress(properties.getProperty("fromMail")));
                mimeMessage.setRecipients(Message.RecipientType.TO, address);
                String subjects = MimeUtility.encodeWord(headline, "UTF-8", "Q");
                mimeMessage.setSubject(subjects);
                getHTML(reportInfo);
                mimeMessage.setContent("测试报告已发送到D盘,文件名为：test.html", "text/html;charset=UTF-8");
                transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
                transport.close();
                log.info("【邮件投递成功】");
            } catch (MessagingException e) {
                e.printStackTrace();
                log.error("异常信息抛出：【{},{}】",e.getCause(),e.getMessage());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                log.error("异常信息抛出：【{},{}】",e.getCause(),e.getMessage());
            }
        //});
        //thread.start();
    }

    public static Template getHTML(ReportInfo reportInfo){
        try {
            Configuration configuration = new Configuration();
            configuration.setEncoding(Locale.CHINA,"utf-8");
            configuration.setClassicCompatible(true);
            configuration.setDirectoryForTemplateLoading(ResourceUtils.getFile("classpath:templates"));
            Template template = configuration.getTemplate("index.html");
            Map<String,Object> map = new HashMap<>();
            map.put("response",reportInfo);
            Writer out = new FileWriter(new File("D:\\test.html"));
            template.process(map,out);
            out.close();
            return template;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
